<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="老师姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帮扶优先级" prop="priority">
        <el-input
          v-model="queryParams.priority"
          placeholder="请输入帮扶优先级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联账号" prop="associatedAccount">
        <el-input
          v-model="queryParams.associatedAccount"
          placeholder="请输入关联账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:management:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:management:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:management:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:management:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="老师姓名" align="center" prop="teacherName" />
      <el-table-column label="学科" align="center" prop="discipline" >
        <template slot-scope="scope">
          <el-popover placement="top">
            <div slot="reference" >
              <el-tag size="medium">{{ scope.row.discipline }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="校区" align="center" prop="campus">
        <template slot-scope="scope">
          <el-popover placement="top">
            <div slot="reference" >
              <el-tag size="medium">{{ scope.row.campus }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="帮扶优先级" align="center" prop="priority" />
      <el-table-column label="老师类型" align="center" prop="type" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="formatSex" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:management:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:management:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老师管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="校区" prop="campus">
          <el-select v-model="form.schoolId" clearable placeholder="请选择校区">
            <el-option
              v-for="item in schoolList"
              :key="item.id"
              :label="item.campus"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科" prop="discipline">
          <el-select v-model="form.disciplineId" clearable placeholder="请选择学科">
            <el-option
              v-for="item in disciplineList"
              :key="item.id"
              :label="item.discipline"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老师类型" prop="type">
          <el-select v-model="form.typeId" clearable placeholder="请选择老师类型">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.type"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老师姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入老师姓名" />
        </el-form-item>
        <el-form-item label="老师性别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入老师姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="账号" prop="associatedAccount">
          <el-input v-model="form.associatedAccount" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="帮扶优先级" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入帮扶优先级" />
        </el-form-item>
        <el-form-item label="上级组长" prop="headman">
          <el-select v-model="form.managementSuperiorId" clearable placeholder="请选择上级组长">
            <el-option
              v-for="item in superiorList"
              :key="item.id"
              :label="item.headman"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绩效类型" prop="performanceTypeId">
          <el-select v-model="typeId" clearable placeholder="请选择绩效类型">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.label"
              :value="item.campus">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSchool,
  listManagement,
  getManagement,
  delManagement,
  addManagement,
  updateManagement, listDiscipline, listType, listSuperior,
} from "@/api/teacher/management";

export default {
  name: "Management",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师管理表格数据
      managementList: [],
      //校区
      schoolList: [],
      //学科
      disciplineList: [],
      //老师类型
      typeList: [],
      //上级组长
      superiorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        phone: null,
        disciplineId: null,
        schoolId: null,
        priority: null,
        typeId: null,
        sex: null,
        performanceTypeId: null,
        managementSuperiorId: null,
        associatedAccount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getSchoolList();
    this.getDisciplineList();
    this.getTypeList();
    this.getSuperiorList();
  },
  methods: {
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 0 ? '女' : row.sex == 1 ? '男' : '未知';
    },
    /** 查询老师管理列表 */
    getList() {
      this.loading = true;
      listManagement(this.queryParams).then(response => {
        this.managementList = response.rows;
        console.log(response)
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询校区列表 */
    getSchoolList() {
      this.loading = true;
      listSchool(this.queryParams).then(response => {
        this.schoolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询学科列表 */
    getDisciplineList() {
      this.loading = true;
      listDiscipline(this.queryParams).then(response => {
        this.disciplineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询老师类型列表 */
    getTypeList() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询上级组长列表 */
    getSuperiorList() {
      this.loading = true;
      listSuperior(this.queryParams).then(response => {
        this.superiorList = response.rows;
        console.log(this.superiorList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherName: null,
        phone: null,
        disciplineId: null,
        schoolId: null,
        priority: null,
        typeId: null,
        sex: null,
        performanceTypeId: null,
        managementSuperiorId: null,
        associatedAccount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加老师管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getManagement(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老师管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除老师管理编号为"' + ids + '"的数据项？').then(function() {
        return delManagement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/management/export', {
        ...this.queryParams
      }, `management_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
